<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <RequiresProcessIsolation>true</RequiresProcessIsolation>
    <ReferenceXUnitWrapperGenerator>false</ReferenceXUnitWrapperGenerator>
    <CrossGenTest>false</CrossGenTest>
    <!-- https://github.com/dotnet/runtime/issues/73138 -->
    <IlasmRoundTripIncompatible>true</IlasmRoundTripIncompatible>
    <!-- This test launches crossgen2 with dotnet, so we would need a non-sanitzed jitinterface library. To simplify our infrastructure, we'll instead skip this test. -->
    <CLRTestTargetUnsupported Condition="'$(EnableNativeSanitizers)' != ''">true</CLRTestTargetUnsupported>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="fieldgetter.ilproj" />
    <ProjectReference Include="testv1\test.csproj">
      <Project>{F74F55A1-DFCF-4C7C-B462-E96E1D0BB667}</Project>
    </ProjectReference>
    <CMakeProjectReference Include="CMakeLists.txt" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="main.cs" />
  </ItemGroup>
  <PropertyGroup>
    <CLRTestBatchPreCommands><![CDATA[
$(CLRTestBatchPreCommands)

REM Suppress some DOTNET and COMPlus variables for the duration of Crossgen2 execution
setlocal
set "DOTNET_GCName="
set "DOTNET_GCStress="
set "DOTNET_HeapVerify="
set "DOTNET_ReadyToRun="

md IL_DLLS
if not exist IL_DLLS\fieldgetter.dll (
    copy /y fieldgetter.dll IL_DLLS\fieldgetter.dll
)
if not exist IL_DLLS\fieldgetter.dll (
    echo FAILED to copy fieldgetter.dll to IL_DLLS
    exit /b 1
)
if not exist IL_DLLS\mainv1.dll (
    copy /y mainv1.dll IL_DLLS\mainv1.dll
)
if not exist IL_DLLS\mainv1.dll (
    echo FAILED to copy mainv1.dll to IL_DLLS
    exit /b 1
)
IF "%__TestDotNetCmd%"=="" set __TestDotNetCmd=dotnet

%__TestDotNetCmd% %Core_Root%\crossgen2\crossgen2.dll --map -r:%Core_Root%\*.dll -r:%25CD%\IL_DLLS\fieldgetter.dll -r:%25CD%\IL_DLLS\mainv1.dll -o:test.dll testv1\test.dll

set CrossGenStatus=!ERRORLEVEL!
IF NOT !CrossGenStatus!==0 (
    ECHO Crossgen failed with exitcode - !CrossGenStatus!
    Exit /b 1
)
if not exist test.map (
    echo FAILED to build test.dll
    exit /b 1
)

%__TestDotNetCmd% %Core_Root%\crossgen2\crossgen2.dll --map -r:%Core_Root%\*.dll -r:%25CD%\IL_DLLS\mainv1.dll -r:testv1\test.dll -o:fieldgetter.dll IL_DLLS\fieldgetter.dll

set CrossGenStatus=!ERRORLEVEL!
IF NOT !CrossGenStatus!==0 (
    ECHO Crossgen failed with exitcode - !CrossGenStatus!
    Exit /b 1
)
if not exist fieldgetter.map (
    echo FAILED to build fieldgetter.dll
    exit /b 1
)

%__TestDotNetCmd% %Core_Root%\crossgen2\crossgen2.dll --map -r:%Core_Root%\*.dll -r:%25CD%\IL_DLLS\fieldgetter.dll -r:testv1\test.dll --opt-cross-module:test -o:mainv1.dll IL_DLLS\mainv1.dll

set CrossGenStatus=!ERRORLEVEL!
IF NOT !CrossGenStatus!==0 (
    ECHO Crossgen failed with exitcode - !CrossGenStatus!
    Exit /b 1
)
if not exist mainv1.map (
    echo FAILED to build mainv1.dll
    exit /b 1
)

endlocal
]]></CLRTestBatchPreCommands>
    <CLRTestBashPreCommands><![CDATA[
$(CLRTestBashPreCommands)

# Suppress some DOTNET variables for the duration of Crossgen2 execution
export -n DOTNET_GCName DOTNET_GCStress DOTNET_HeapVerify DOTNET_ReadyToRun

mkdir IL_DLLS

if [ ! -f IL_DLLS/fieldgetter.dll ]
then
  cp fieldgetter.dll IL_DLLS/fieldgetter.dll
fi
if [ ! -f IL_DLLS/fieldgetter.dll ]
then
  echo Failed to copy fieldgetter.dll to IL_DLLS
  exit 1
fi

if [ ! -f IL_DLLS/mainv1.dll ]
then
  cp mainv1.dll IL_DLLS/mainv1.dll
fi
if [ ! -f IL_DLLS/mainv1.dll ]
then
  echo Failed to copy mainv1.dll to IL_DLLS
  exit 1
fi

$CORE_ROOT/corerun $CORE_ROOT/crossgen2/crossgen2.dll --map -r:$CORE_ROOT/*.dll -r:IL_DLLS/fieldgetter.dll -r:IL_DLLS/mainv1.dll -o:test.dll testv1/test.dll

__cgExitCode=$?
if [ $__cgExitCode -ne 0 ]
then
  echo Crossgen failed with exitcode: $__cgExitCode
  exit 1
fi
if [ ! -f test.map ]
then
  echo Failed to build test.dll
  exit 1
fi

$CORE_ROOT/corerun $CORE_ROOT/crossgen2/crossgen2.dll --map -r:$CORE_ROOT/*.dll -r:testv1/test.dll -r:IL_DLLS/mainv1.dll -o:fieldgetter.dll IL_DLLS/fieldgetter.dll

__cgExitCode=$?
if [ $__cgExitCode -ne 0 ]
then
  echo Crossgen failed with exitcode: $__cgExitCode
  exit 1
fi
if [ ! -f fieldgetter.map ]
then
  echo Failed to build fieldgetter.dll
  exit 1
fi

$CORE_ROOT/corerun $CORE_ROOT/crossgen2/crossgen2.dll --map -r:$CORE_ROOT/*.dll -r:testv1/test.dll -r:IL_DLLS/fieldgetter.dll --opt-cross-module:test -o:mainv1.dll IL_DLLS/mainv1.dll

__cgExitCode=$?
if [ $__cgExitCode -ne 0 ]
then
  echo Crossgen failed with exitcode: $__cgExitCode
  exit 1
fi
if [ ! -f mainv1.map ]
then
  echo Failed to build mainv1.dll
  exit 1
fi

export DOTNET_GCName DOTNET_GCStress DOTNET_HeapVerify DOTNET_ReadyToRun
]]></CLRTestBashPreCommands>
  </PropertyGroup>

  <Target Name="CopyTestAssemblyToOutputs" AfterTargets="Build" Condition="'$(CLRTestTargetUnsupported)' != 'true'">
    <PropertyGroup>
      <TestAssemblyName>test.dll</TestAssemblyName>
      <TestAssemblySourcePath>$(OutputPath)/../testv1/test/</TestAssemblySourcePath>
      <TestAssemblyTargetPath>$(OutputPath)/testv1</TestAssemblyTargetPath>
    </PropertyGroup>
    
    <MakeDir Directories="$(TestAssemblyTargetPath)" />
    <Copy SourceFiles="$(TestAssemblySourcePath)$(TestAssemblyName)" DestinationFolder="$(TestAssemblyTargetPath)" />
  </Target>
</Project>
